package com.taokatao.cardmee.business.profile;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.taokatao.cardmee.App;
import com.taokatao.cardmee.BaseActivity;
import com.taokatao.cardmee.BusEvent;
import com.taokatao.cardmee.C;
import com.taokatao.cardmee.R;
import com.taokatao.cardmee.Utils;
import com.taokatao.cardmee.api.ApiManager;
import com.taokatao.cardmee.api.RespInfo;
import com.taokatao.cardmee.api.RespWrapper;
import com.taokatao.cardmee.api.SubscriberAdapter;
import com.taokatao.cardmee.api.model.SecretCardInfo;
import com.taokatao.cardmee.business.cardcrud.InfoEditActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class ProfileReceiveInfoActivity extends BaseActivity implements View.OnClickListener {
    @BindView(R.id.l_realname) View lRealname;
    @BindView(R.id.t_realname) TextView tRealName;

    @BindView(R.id.t_submit) TextView tSubmit;

    @BindView(R.id.i_background) ImageView iBackground;
    @BindView(R.id.i_logo) ImageView iLogo;
    @BindView(R.id.t_grade) TextView tGrade;
    @BindView(R.id.t_card_name) TextView tCardName;
    @BindView(R.id.i_code) ImageView iCode;
    @BindView(R.id.t_number) TextView tNumber;

    private int styleColor;
    private SecretCardInfo c;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile_receive_info);
        ButterKnife.bind(this);

        c = (SecretCardInfo) getIntent().getSerializableExtra("extra_secret_info");
        resetBasicInfo();

        tRealName.setText(App.i().getCurrUser().getRealName());
    }
    static final int REQUEST_CODE_PROFILE_EDIT_REALNAME = 1033;

    private void resetBasicInfo() {

        if (!TextUtils.isEmpty(c.backColor) && c.backColor.length() == 7 && c.backColor.startsWith("#")) {
            styleColor = Color.parseColor(c.backColor);
        } else {
            styleColor = 0xFF4C4C4C;
        }
        tCardName.setText(c.businessName);
        if (!TextUtils.isEmpty(c.sname)) {
            tCardName.setText(c.sname);
        }

        if (!TextUtils.isEmpty(c.mCardIcon)) {
            iLogo.setVisibility(View.VISIBLE);
            Glide.with(iLogo.getContext()).load(c.mCardIcon).into(iLogo);
        }


        boolean blackColorStyle = Utils.isDarkColorTheme(styleColor);
        if (!blackColorStyle) {
            styleColor = 0xFF242424;
        }
        iBackground.setImageDrawable(new ColorDrawable(styleColor));
        iCode.setImageResource(blackColorStyle ? R.mipmap.card_code_white : R.mipmap.card_code_black);
        String cardNumber = TextUtils.isEmpty(c.consumeCardCode) ? "" : c.consumeCardCode;
        tNumber.setText("NO." + Utils.formatCardCode(cardNumber));

        if (!TextUtils.isEmpty(c.gradeName) && !TextUtils.equals("普通卡", c.gradeName)) {
            tGrade.setVisibility(View.VISIBLE);
            tGrade.setText(c.gradeName);
        } else {
            tGrade.setVisibility(View.GONE);
        }
    }

    @Override
    @OnClick({R.id.t_submit,R.id.l_realname})
    public void onClick(View v) {
        if (v == tSubmit) {
            tSubmit.setEnabled(false);
            secretToAddCardConfirm(c.id);
        } else if (v == lRealname) {
            Intent intent = new Intent(this, InfoEditActivity.class);
            intent.putExtra("extra_content", tRealName.getText().toString());
            intent.putExtra("extra_max_length", 10);
            startActivityForResult(intent, REQUEST_CODE_PROFILE_EDIT_REALNAME);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_PROFILE_EDIT_REALNAME) {
            if (resultCode == RESULT_OK) {
                String result = data.getStringExtra("result");
                tRealName.setText(result);
            }
        }
    }

    private void secretToAddCardConfirm(long id) {
        final String mInputRealName = tRealName.getText().toString();
        ApiManager.i().secretToAddCardConfirm(this, App.i().getCurrUser().getUuid(), id , mInputRealName, new SubscriberAdapter<RespWrapper<RespInfo>>() {
            @Override
            public void onNext(RespWrapper<RespInfo> o) {
                super.onNext(o);
                if (o.code == C.http.RESULT_CODE_SUCCESS) {
                    Utils.postMainThreadBusEvent(BusEvent.CARD_REFRESH);
                    Intent intent = new Intent(ProfileReceiveInfoActivity.this, ProfileReceiveCompleteActivity.class);
                    startActivity(intent);
                }
            }
        }.showProgress(this).submitDisable(tSubmit));
    }
}
